//
// Created by 32827 on 2024/1/24.
//
#include "yzl_ui.h"
#ifdef YZL_UI_TLI4384
#include "yzl_tli4384.h"
YZF_RESULT yzl_ui_init(YZ_COLOR background){
    YZL_UI_Background=background;
    YZL_UI_IS_INIT=YZF_TRUE;
    return yzl_ui_fill_screen(YZL_UI_Background);
}
YZF_RESULT yzl_ui_clear_screen(void){
    return yzl_ui_fill_screen(YZL_UI_Background);
}
YZF_RESULT yzl_ui_fill_screen(YZ_COLOR color){
    YZ_COLOR *ScreenColor=(void *)YZL_TLI4384_RAM_ADD;
    for(int y=0;y<YZ_SCREEN_HEIGHT;y++){
        for (int i = 0; i < YZ_SCREEN_WIDTH; i++) {
            ScreenColor[y*YZ_SCREEN_WIDTH+i]=color;
        }
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_rect(uint16_t x,uint16_t y,uint16_t width,uint16_t height,YZF_BOOL isFill,YZ_COLOR color){
    YZ_COLOR *ScreenColor=(void *)YZL_TLI4384_RAM_ADD;
    if(x<0||y<0){
        return YZF_ERROR;
    }
    if(isFill){
        for(int iy=y;iy<((y+height)<=YZ_SCREEN_HEIGHT?(y+height):YZ_SCREEN_HEIGHT);iy++){
            for (int ix = x; ix < ((x+width)<=YZ_SCREEN_WIDTH?(x+width):YZ_SCREEN_WIDTH); ix++) {
                ScreenColor[iy*YZ_SCREEN_WIDTH+ix]=color;
            }
        }
    }else{
        yzl_ui_draw_line(x,y,x+width,y,color);
        yzl_ui_draw_line(x,y,x,y+height,color);
        yzl_ui_draw_line(x,y+height,x+width,y+height,color);
        yzl_ui_draw_line(x+width,y,x+width,y+height,color);
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_circle(uint16_t x,uint16_t y,uint16_t radius,YZF_BOOL isFill,YZ_COLOR color){
    int16_t sCurrentX, sCurrentY;
    int16_t sError;


    sCurrentX = 0; sCurrentY = radius;

    sError = 3 - ( radius << 1 );     //判断下个点位置的标志


    while ( sCurrentX <= sCurrentY )
    {
        int16_t sCountY;


        if ( isFill )
            for ( sCountY = sCurrentX; sCountY <= sCurrentY; sCountY ++ )
            {
                yzl_ui_draw_pixel(x + sCurrentX, y + sCountY, color);           //1，研究对象
                yzl_ui_draw_pixel(x - sCurrentX, y + sCountY, color);           //2
                yzl_ui_draw_pixel(x - sCountY, y + sCurrentX, color);           //3
                yzl_ui_draw_pixel(x - sCountY, y - sCurrentX, color);           //4
                yzl_ui_draw_pixel(x - sCurrentX, y - sCountY, color);           //5
                yzl_ui_draw_pixel(x + sCurrentX, y - sCountY, color);           //6
                yzl_ui_draw_pixel(x + sCountY, y - sCurrentX, color);           //7
                yzl_ui_draw_pixel(x + sCountY, y + sCurrentX, color);           //0
            }

        else
        {
            yzl_ui_draw_pixel(x + sCurrentX, y + sCurrentY, color);             //1，研究对象
            yzl_ui_draw_pixel(x - sCurrentX, y + sCurrentY, color);             //2
            yzl_ui_draw_pixel(x - sCurrentY, y + sCurrentX, color);             //3
            yzl_ui_draw_pixel(x - sCurrentY, y - sCurrentX, color);             //4
            yzl_ui_draw_pixel(x - sCurrentX, y - sCurrentY, color);             //5
            yzl_ui_draw_pixel(x + sCurrentX, y - sCurrentY, color);             //6
            yzl_ui_draw_pixel(x + sCurrentY, y - sCurrentX, color);             //7
            yzl_ui_draw_pixel(x + sCurrentY, y + sCurrentX, color);             //0
        }


        sCurrentX ++;


        if ( sError < 0 )
            sError += 4 * sCurrentX + 6;

        else
        {
            sError += 10 + 4 * ( sCurrentX - sCurrentY );
            sCurrentY --;
        }
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_line(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,YZ_COLOR color){
    uint16_t us;
    uint16_t usX_Current, usY_Current;

    int32_t lError_X = 0, lError_Y = 0, lDelta_X, lDelta_Y, lDistance;
    int32_t lIncrease_X, lIncrease_Y;


    lDelta_X = x2 - x1; //计算坐标增量
    lDelta_Y = y2 - y1;

    usX_Current = x1;
    usY_Current = y1;


    if ( lDelta_X > 0 )
        lIncrease_X = 1; //设置单步方向

    else if ( lDelta_X == 0 )
        lIncrease_X = 0;//垂直线

    else
    {
        lIncrease_X = -1;
        lDelta_X = - lDelta_X;
    }


    if ( lDelta_Y > 0 )
        lIncrease_Y = 1;

    else if ( lDelta_Y == 0 )
        lIncrease_Y = 0;//水平线

    else
    {
        lIncrease_Y = -1;
        lDelta_Y = - lDelta_Y;
    }


    if (  lDelta_X > lDelta_Y )
        lDistance = lDelta_X; //选取基本增量坐标轴

    else
        lDistance = lDelta_Y;


    for ( us = 0; us <= lDistance + 1; us ++ )//画线输出
    {
        yzl_ui_draw_pixel(usX_Current, usY_Current, color);//画点

        lError_X += lDelta_X ;
        lError_Y += lDelta_Y ;

        if ( lError_X > lDistance )
        {
            lError_X -= lDistance;
            usX_Current += lIncrease_X;
        }

        if ( lError_Y > lDistance )
        {
            lError_Y -= lDistance;
            usY_Current += lIncrease_Y;
        }

    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_pixel(uint16_t x1,uint16_t y1,YZ_COLOR color){
    ((YZ_COLOR *)YZL_TLI4384_RAM_ADD)[y1*YZ_SCREEN_WIDTH+x1]=color;
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_pixels_ex(uint16_t x1,uint16_t y1,uint16_t width,uint16_t height,YZ_COLOR *img,YZ_COLOR alphaColor){
    YZ_COLOR *ScreenColor=(void *)YZL_TLI4384_RAM_ADD;
    for(int iy=y1;iy<((y1+height)<=YZ_SCREEN_HEIGHT?(y1+height):YZ_SCREEN_HEIGHT);iy++){
        for (int ix = x1; ix < ((x1+width)<=YZ_SCREEN_WIDTH?(x1+width):YZ_SCREEN_WIDTH); ix++) {
            if(img[(iy-y1)*width+(ix-x1)].data==alphaColor.data){
                continue;
            }
            ScreenColor[iy*YZ_SCREEN_WIDTH+ix]=img[(iy-y1)*width+(ix-x1)];
        }
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_pixels(uint16_t x1,uint16_t y1,uint16_t width,uint16_t height,YZ_COLOR *img){
    YZ_COLOR *ScreenColor=(void *)YZL_TLI4384_RAM_ADD;
    for(int iy=y1;iy<((y1+height)<=YZ_SCREEN_HEIGHT?(y1+height):YZ_SCREEN_HEIGHT);iy++){
        for (int ix = x1; ix < ((x1+width)<=YZ_SCREEN_WIDTH?(x1+width):YZ_SCREEN_WIDTH); ix++) {
            ScreenColor[iy*YZ_SCREEN_WIDTH+ix]=img[(iy-y1)*width+(ix-x1)];
        }
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_draw_string(uint16_t x,uint16_t y,uint16_t limitX,uint16_t *str,uint16_t len,YZ_COLOR back_color,YZ_COLOR text_color){
    if(Yz_FontInitFlag!=YZF_TRUE){
        return YZF_ERROR;
    }
    uint16_t startX=x,startY=y;
    uint32_t offx,offy;
    int tempLen=len;
    uint16_t wlen = 0;
    while (len!=0) {
        if(len>YZL_FONT_SRAM_SIZE){
            wlen=YZL_FONT_SRAM_SIZE;
            len-=YZL_FONT_SRAM_SIZE;
        }else{
            wlen=len;
            len=0;
        }
        if (yzl_font_get_pixels(&str[tempLen-len-wlen], yzl_font_sram, wlen) == YZF_OK) {
            for (int m = 0; m < wlen; m++) {
                if(str[tempLen-len-wlen+m]=='\000'){
                    continue;
                }else
                if(str[tempLen-len-wlen+m]=='\r'){
                    startX=0;
                    continue;
                }else
                if(str[tempLen-len-wlen+m]=='\n'){
                    startY+=YZL_FONT_LINE_HEIGHT;
                    startX=x;
                    continue;
                }

                if (startX + yzl_font_sram[m].xOffset < 0 || (startY + yzl_font_sram[m].yOffset) < 0) {
                    offx=startX;
                    offy=startY;
                } else {
                    offx=startX+yzl_font_sram[m].xOffset;
                    offy=startY+yzl_font_sram[m].yOffset;
                }
                int count = 0;
                int pixelIndex = 0;
                for (int i = 0; i < yzl_font_sram[m].height; i++) {
                    for (int j = 0; j < yzl_font_sram[m].width; j++) {
                        if (count >= 8) {
                            pixelIndex++;
                            count = 0;
                        }
                        if ((yzl_font_sram[m].pixel[pixelIndex] & (0b1 << (7 - count))) != 0) {
                            yzl_ui_draw_pixel(offx + j, offy + i, text_color);
                        } else {
//                            yzl_ui_draw_pixel(offx + j, offy + i, back_color);
                        }
                        count++;
                    }
                }

                startX = startX + yzl_font_sram[m].xAdvance;

                if (startX + YZL_FONT_SIZE > limitX) {
                    startY+=YZL_FONT_LINE_HEIGHT;
                    startX=x;
                }
            }
        } else {
            return YZF_ERROR;
        }
    }
    return YZF_OK;
}
YZF_RESULT yzl_ui_get_pixel(uint16_t x,uint16_t y,YZ_COLOR *color){
    *color=*(((YZ_COLOR *)YZL_TLI4384_RAM_ADD)+(y*YZ_SCREEN_WIDTH+x));
    return YZF_OK;
}
YZF_RESULT yzl_ui_get_pixels_line(uint16_t x,uint16_t y,uint16_t width,YZ_COLOR *color){
    //TODO: Can Change that to use DMA.
    memcpy(color,(((YZ_COLOR *)YZL_TLI4384_RAM_ADD)+(y*YZ_SCREEN_WIDTH+x)),sizeof(YZ_COLOR)*width);
    return YZF_OK;
}
#endif